<template>
  <el-dialog
    v-model="visible"
    title="编辑媒体文件"
    width="600px"
    @close="handleClose"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="文件名" prop="name">
        <el-input v-model="form.name" placeholder="请输入文件名" />
      </el-form-item>
      
      <el-form-item label="分类" prop="category">
        <el-select v-model="form.category" placeholder="请选择分类" style="width: 100%">
          <el-option label="电影" value="电影" />
          <el-option label="音乐" value="音乐" />
          <el-option label="教育" value="教育" />
          <el-option label="新闻" value="新闻" />
          <el-option label="娱乐" value="娱乐" />
        </el-select>
      </el-form-item>
      
      <el-form-item label="标签" prop="tags">
        <el-select
          v-model="form.tags"
          multiple
          filterable
          allow-create
          placeholder="请选择或输入标签"
          style="width: 100%"
        >
          <el-option label="预告片" value="预告片" />
          <el-option label="MV" value="MV" />
          <el-option label="教程" value="教程" />
          <el-option label="新闻" value="新闻" />
          <el-option label="音乐" value="音乐" />
          <el-option label="电影" value="电影" />
        </el-select>
      </el-form-item>
      
      <el-form-item label="描述">
        <el-input
          v-model="form.description"
          type="textarea"
          :rows="4"
          placeholder="请输入文件描述"
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSave" :loading="saving">
          保存
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, computed, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { mediaApi } from '@/api/modules/media'
import type { MediaFile } from '@/types'

interface Props {
  modelValue: boolean
  media?: MediaFile
}

interface Emits {
  (e: 'update:modelValue', value: boolean): void
  (e: 'updated'): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()

const formRef = ref()
const saving = ref(false)

const visible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

const form = reactive({
  name: '',
  category: '',
  tags: [] as string[],
  description: ''
})

const rules = {
  name: [
    { required: true, message: '请输入文件名', trigger: 'blur' }
  ],
  category: [
    { required: true, message: '请选择分类', trigger: 'change' }
  ]
}

// 监听媒体数据变化
watch(() => props.media, (media) => {
  if (media) {
    form.name = media.name
    form.category = media.category
    form.tags = [...media.tags]
    form.description = media.description || ''
  }
}, { immediate: true })

// 保存
const handleSave = async () => {
  if (!props.media) return
  
  try {
    await formRef.value.validate()
    
    saving.value = true
    
    await mediaApi.updateMediaFile(props.media.id, {
      name: form.name,
      category: form.category,
      tags: form.tags,
      description: form.description
    })
    
    ElMessage.success('保存成功')
    emit('updated')
    visible.value = false
  } catch (error) {
    if (error !== false) { // 不是表单验证错误
      ElMessage.error('保存失败')
    }
  } finally {
    saving.value = false
  }
}

// 关闭
const handleClose = () => {
  formRef.value?.resetFields()
  visible.value = false
}
</script> 